<!-- 开具处方 -->
<template>
  <ToolbarItemContainer :iconFile="Icon" title="开具处方" :needBottomPopup="true"
    :iconWidth="isUniFrameWork ? '26px' : '20px'" :iconHeight="isUniFrameWork ? '26px' : '20px'" :needDialog="false"
    @onIconClick="onIconClick" ref="container" />
</template>
<script setup lang="ts">
import { TUIStore, StoreName, IConversationModel } from '@tencentcloud/chat-uikit-engine'
import { ref } from '@/TUIKit/adapter-vue'
import ToolbarItemContainer from '@/TUIKit/components/TUIChat/message-input-toolbar/toolbar-item-container/index.vue'
import { isPC, isUniFrameWork } from '@/TUIKit/utils/env'
import Icon from '@/TUIWidget/static/icon/prescription.png'
import { getOutpatientInfo } from '@/api/ConsultationRoom'

const currentConversation = ref<IConversationModel>()
const container = ref()

TUIStore.watch(StoreName.CONV, {
  currentConversation: (conversation: IConversationModel) => {
    currentConversation.value = conversation
  },
})

// 获取问诊详情
const GetOutpatientInfo = async id => {
  const { data } = await getOutpatientInfo({ id })
  return data
}

/** 点击事件 */
const onIconClick = async () => {
  console.log('-- 开具处方 ---------------->', consultation.value)

  const { id, patientId, doctorId, groupId } = consultation.value


  // 测试用
  // uni.navigateTo({
  //   url: `/pages/ConsultationRoom/Prescription/Prescription?id=${id}&groupId=${groupId}`,
  // })


  // 获取会诊详情(用于判断是否完成了必要操作)
  try {
    const outpatientInfo = await GetOutpatientInfo(consultation.value.id)
    console.log('outpatientInfo', outpatientInfo)

    if (!outpatientInfo.medicalRecordId) {
      console.log('medicalRecordId', outpatientInfo.medicalRecordId)
      uni.showModal({
        title: `提示`,
        content: `当前暂不支持开方，需为患者整理病历并等待患者确认后才能进行开处方。`,
        confirmText: '整理病历',
        cancelText: '关闭',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')

            uni.navigateTo({
              url: `/pages/ConsultationRoom/medicalRecord/medicalRecord?id=${consultation.value.id}&groupId=${consultation.value.groupId}`,
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
      return
    }
    if (!outpatientInfo.userConfirm) {
      console.log('userConfirm', outpatientInfo.userConfirm)
      uni.showModal({
        title: `提示`,
        content: `当前暂不支持开方，请等待患者确认病历后进行开处方`,
        confirmText: '去沟通',
        cancelText: '关闭',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
      return
    }

    uni.navigateTo({
      url: `/pages/ConsultationRoom/Prescription/Prescription?id=${consultation.value.id}`,
    })
  } catch (error) {
    uni.showToast({
      title: error,
      duration: 2000,
      icon: 'none',
    })
  }
}

// 获取自定义数据
const consultation = ref<object>({})
// UI 层监听当前更新通知
let onCurrentGroupUpdated = function (currentGroup) {
  if (!currentGroup?.groupCustomField) return

  // 设置会话自定义字段
  consultation.value = JSON.parse(
    currentGroup.groupCustomField.find((item: any) => item.key === 'consultation').value || 0
  )
}

// 监听当前会话ID更新
TUIStore.watch(StoreName.GRP, {
  currentGroup: onCurrentGroupUpdated,
})
</script>
<style scoped lang="scss" src="./style/index.scss"></style>
